package com.dim.table;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 解析高德API区域划分信息，获取省市区县维表
 *
 * @author zhangchenguang
 * 2019年11月20日
 */
public class ParseJson {

    public static void main(String[] args) throws Exception {
        File file = new File("C:\\Users\\ASUS\\Desktop\\district.json");
        StringBuilder result = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8"));
        String s = null;
        while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
            result.append(System.lineSeparator() + s);
        }
        br.close();
//        System.out.println(result.toString());
        JSONObject json1 = JSONObject.parseObject(result.toString());
        JSONArray ja1 = (JSONArray)json1.get("districts");
        JSONObject json2 = (JSONObject)ja1.get(0);
        JSONArray ja2 = (JSONArray)json2.get("districts");

        // System.out.println(ja2.toJSONString());

        ArrayList<String> arrs = new ArrayList<String>() ;
        Iterator<Object> its = ja2.iterator();
        while(its.hasNext()){
            JSONObject tmp = (JSONObject)its.next();
            String prov_id = (String)tmp.get("adcode");
            String prov_name = (String)tmp.get("name");

            // 存到数组里面构建表
            arrs.add(prov_id+","+prov_name+","+0+","+prov_name);
            // System.out.println("省级：" + prov_id+"====="+prov_name + "===" + 0 + "===" + prov_name);
            JSONArray ja3 = (JSONArray) tmp.get("districts");
            for (Object t_json : ja3.toArray()) {
                JSONObject js_tmp = (JSONObject)t_json;
                String city_id = (String)js_tmp.get("adcode");
                String city_name = (String)js_tmp.get("name");
                // 存到数组里面构建表
                arrs.add(city_id+","+city_name+","+prov_id+","+prov_name+"_"+city_name);
                // System.out.println("\t市级：" + city_id + "====="+city_name + "===" + prov_id + "===" + prov_name+"_"+city_name);
                JSONArray ja4 = (JSONArray) js_tmp.get("districts");
                if(ja4==null || ja4.size() == 0){
                    break;
                }
                for (Object t4 : ja4.toArray()) {
                    JSONObject js_tmp4 = (JSONObject)t4;
                    String dirt_id = (String)js_tmp4.get("adcode");
                    String dirt_name = (String)js_tmp4.get("name");
                    // 存到数组里面构建表
                    arrs.add(dirt_id+","+dirt_name+","+city_id+","+prov_name+"_"+city_name+"_"+dirt_name);
                    // System.out.println("\t\t区级：" + dirt_id+"====="+dirt_name + "===" + city_id + "===" + prov_name+"_"+city_name+"_"+dirt_name);
                }
            }
        }

        System.out.println("====================完美输出~====================");
        for (String str : arrs) {
           // fos.write(str);
            System.out.println(str);
        }
        //System.out.println(arrs);
    }
}

